<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\CategoryRequest;
use App\Models\Catergory;
use Illuminate\Http\Request;

class CatergoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Catergory $catergory)
    {
         $categories = $catergory->paginate(5);
        return view('admin.catergory_index',compact('categories'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Catergory $catergory)
    {
        $categories = $catergory->getAllCategory();
        return view('admin.category_create',compact('categories'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CategoryRequest $request)
    {
//        dd($request->all());
        if (!$request['icon']){
            $request['icon'] = 'fe fe-home';
        }
        Catergory::create($request->all());
        return redirect(route('admin.catergory.index'))->with('success','栏目创建成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Catergory  $catergory
     * @return \Illuminate\Http\Response
     */
    public function show(Catergory $catergory)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Catergory  $catergory
     * @return \Illuminate\Http\Response
     */
    public function edit(Catergory $catergory)
    {
        $categories = $catergory->getAllCategory();
        return view('admin.category_edit',compact('categories','catergory'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Catergory  $catergory
     * @return \Illuminate\Http\Response
     */
    public function update(CategoryRequest $request, Catergory $catergory)
    {
        if (!$request['icon']){
            $request['icon'] = 'fe fe-home';
        }
        $catergory->update($request->all());
        return redirect(route('admin.catergory.index'))->with('success','修改成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Catergory  $catergory
     * @return \Illuminate\Http\Response
     */
    public function destroy(Catergory $catergory)
    {
       $catergory->delete();
       return redirect(route('admin.catergory.index'))->with('success','删除成功');
    }
}
